function tran(data,fields,dict){
    if(!data)return;
    Object.keys(fields).forEach(key=>{
        data.forEach(row=>{
            if(!row.tran)row.tran = {};
            if(dict[fields[key]]){
                const tranItem = dict[fields[key]].filter(m=>m.value===row[key]);
                row.tran[key] = tranItem.length>0?tranItem[0].label:'';
            }
        })
    })
}

module.exports={
    list(data){
        return request({
            url: '/options/'+data,
            headers: {},
            method: 'get'
        });
    },

    useDict:(dict,data)=>{
        request({
            url: '/options/arr/'+data,
            headers: {},
            method: 'get'
        }).then(res=>{
            Object.keys(res.data).forEach(key=>{
                dict.value[key]=res.data[key];
            })
        })
    },
    tranDict:(data,fields,dict)=>{
        if(dict) {
            tran(data,fields,dict);
            return;
        }
        request({
            url: '/options/arr/'+Array.from(new Set(Object.values(fields))).join(','),
            headers: {},
            method: 'get'
        }).then(res=>{
            const dict = {};
            Object.keys(res.data).forEach(key=>{
                dict[key]=res.data[key];
            })
            tran(data,fields,dict);
        })

    },

    tranTable(data,fields){
        if(!data)return;
        const params = [];
        Object.keys(fields).forEach(field=>{
            const ids = new Set();
            data.forEach(row=>{
                ids.add(row[field]);
            })
            params.push({table: fields[field],ids:Array.from(ids).join(',')})
        })
        request({
            url: '/options/table',
            headers: {},
            data:params,
            method: 'post'
        }).then(res=>{
            tran(data,fields,res.data)
        })
    },

    searchTable(table,params){
        return request({
            url: '/options/table/'+table,
            headers: {},
            params:params,
            method: 'get'
        })
    }
}
